(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.fxRate.uploadErrorController', uploadErrorController);
	uploadErrorController.$inject = [ '$scope', 'modalService', 'layoutConfig', 'validatorConfig', 'authService',
			'ajaxServiceFactory' ];
	function uploadErrorController($scope, modalService, layoutConfig, validatorConfig, authService, ajaxServiceFactory) {
		var ctrl = this, taskInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.fxRate = {};

		ctrl.init = function() {
			var params = {
					taskId : taskInfo.taskId
			};
			var ajaxService = ajaxServiceFactory.buildAjaxService({
				commandError : {
					url : 'do/' + taskInfo.serviceCode + '/workflow/commandError/' + taskInfo.dataType,
					method : 'post'
				}
			});
			ajaxService.commandError(params).then(function(response) {
				ctrl.task = response.data.task || {};
				ctrl.fxRate.taskId = response.data.task.taskId;
				ctrl.fxRate.loadId = response.data.task.itemKey;
			});
		};
		
		ctrl.cancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.fxRate.loadId]} ],
				'btnCallback' : {
					'ok' : function() {
						return authService.cancel(ctrl.fxRate).then(submitSuccessful, submitFailed);
					}
				}
			});
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.fxrate.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.fxrate.list',
					'state' : 'cda.fxrate.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
